<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备编号" prop="deviceNumber">
        <el-input
          v-model="queryParams.deviceNumber"
          placeholder="请输入设备编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="捐赠物ID" prop="donationId">
        <el-input
          v-model="queryParams.donationId"
          placeholder="请输入捐赠物ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="货架状态" prop="shelfStatus">
        <el-select v-model="queryParams.shelfStatus" placeholder="请选择货架状态" clearable>
          <el-option
            v-for="dict in goods_shelves_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:shelves:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shelvesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" type="index" width="55px" />
      <el-table-column label="设备编号" align="center" prop="deviceNumber" />
      <el-table-column label="货架号" align="center" prop="shelfNumber" />
      <el-table-column label="货架状态" align="center" prop="shelfStatus">
        <template #default="scope">
          <dict-tag :options="goods_shelves_status" :value="scope.row.shelfStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary"  @click="handleDelete(scope.row)" v-hasPermi="['manage:shelves:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Shelves">
import { listShelves, getShelves, delShelves, addShelves, updateShelves } from "@/api/manage/shelves";
import { ref } from "vue";
import { require } from "@/utils/validate";

const { proxy } = getCurrentInstance();
const { goods_shelves_status } = proxy.useDict('goods_shelves_status');

const shelvesList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deviceNumber: null,
    donationId: null,
    shelfStatus: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);
/** 查询货架管理列表 */
function getList() {
  loading.value = true;
  listShelves(queryParams.value).then(response => {
    shelvesList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    vmId: null,
    deviceNumber: null,
    shelfNumber: null,
    donationId: null,
    shelfStatus: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null
  };
  proxy.resetForm("shelvesRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加货架管理";
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["shelvesRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateShelves(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addShelves(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除货架管理编号为"' + _ids + '"的数据项？').then(function() {
    return delShelves(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/shelves/export', {
    ...queryParams.value
  }, `shelves_${new Date().getTime()}.xlsx`)
}

getList();
</script>
